package com.baseproject.widget;

import android.app.Activity;
import android.content.Context;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.baseproject.R;
import com.baseproject.util.LogUtils;


public class PopupWindowUtils implements View.OnClickListener {

    protected MyPopupWindow mPopupWindow;

    protected View mPopupWindowView;
    private PopupWindowUtilsCallBack mCallBack;
    protected Context mContext;
    protected int mRootId;
    protected int mMainId;

    public PopupWindowUtils(Context context, int layoutId) {
        mContext = context;
        mPopupWindowView = LayoutInflater.from(context).inflate(layoutId, null);
        mPopupWindow = new MyPopupWindow(mPopupWindowView);
        mPopupWindowView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
                    //通过对ACTION_DOWN的判断，解决当触发打开本popupwindow的返回键事件也同时传递到此处，导致又立刻被隐藏的问题
                    return hidePopupWindow(true);
                }
                return false;
            }
        });
        //默认行为
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT)
                .setHeight(ViewGroup.LayoutParams.MATCH_PARENT)
                .setRootId(R.id.popup_root)
                .setMainId(R.id.popup_main)
                .setFocusable(true)
                .setFocusableInTouchMode(true)
                .setOutsideTouchable(true)
                .setAnimationStyle(R.style.popwin_anim_style);
    }

    public <T extends PopupWindowUtils>  T setTitleText(int titleId) {
        String title = mContext.getResources().getString(titleId);
        View titleTV = mPopupWindowView.findViewById(R.id.popup_title_tv);
        if (titleTV != null && titleTV instanceof TextView) {
            ((TextView) titleTV).setText(title);
        } else {
            LogUtils.e("popup_title_tv is null or not textview");
        }
        return (T) this;
    }

    public PopupWindowUtils setText(int textViewId, int textId) {
        TextView textView = (TextView) mPopupWindowView.findViewById(textViewId);
        textView.setText(textId);
        return this;
    }

    public PopupWindowUtils setText(int textViewId, String text) {
        TextView textView = (TextView) mPopupWindowView.findViewById(textViewId);
        textView.setText(text);
        return this;
    }

    public <T extends PopupWindowUtils>  T  setPositiveText(int positiveTextId) {
        String positiveText = mContext.getResources().getString(positiveTextId);
        View positiveBtn = mPopupWindowView.findViewById(R.id.popup_positive_btn);
        if (positiveBtn != null && positiveBtn instanceof TextView) {
            ((TextView) positiveBtn).setText(positiveText);
        } else if (positiveBtn != null && positiveBtn instanceof Button) {
            ((Button) positiveBtn).setText(positiveText);
        } else {
            LogUtils.e("popup_positive_btn is null or type error");
        }
        return (T) this;
    }

    public <T extends PopupWindowUtils> T setPositiveOnClickListener(View.OnClickListener onClickListener) {
        View positiveBtn = mPopupWindowView.findViewById(R.id.popup_positive_btn);
        positiveBtn.setOnClickListener(onClickListener);
        return (T) this;
    }

    public PopupWindowUtils setOnClickListener(int viewId, View.OnClickListener onClickListener) {
        View view = mPopupWindowView.findViewById(viewId);
        view.setOnClickListener(onClickListener);
        return this;
    }

    public MyPopupWindow getPopupWindow() {
        return mPopupWindow;
    }

    public void show(Activity activity) {
        mPopupWindow.showAtLocation(activity.findViewById(android.R.id.content), Gravity.FILL, 0, 0);
    }

    public PopupWindowUtils setCallback(PopupWindowUtilsCallBack callback) {
        mCallBack = callback;
        return this;
    }

    public PopupWindowUtils setRootId(int rootId) {
        mRootId = rootId;
        View rootView = mPopupWindowView.findViewById(mRootId);
        if (null != rootView) {
            rootView.setOnClickListener(this);
        }
        return this;
    }

    public PopupWindowUtils setMainId(int mainId) {
        mMainId = mainId;
        View mainView = mPopupWindowView.findViewById(mMainId);
        if (null != mainView) {
            mainView.setOnClickListener(this);
        }
        return this;
    }

    public PopupWindowUtils setWidth(int width) {
        mPopupWindow.setWidth(width);
        return this;
    }

    public PopupWindowUtils setHeight(int height) {
        mPopupWindow.setHeight(height);
        return this;
    }

    public PopupWindowUtils setFocusable(boolean focusable) {
        mPopupWindow.setFocusable(focusable);
        return this;
    }

    public PopupWindowUtils setAnimationStyle(int animationStyle) {
        mPopupWindow.setAnimationStyle(animationStyle);
        return this;
    }

    public <T extends PopupWindowUtils> T setOutsideTouchable(boolean outsideTouchable) {
        mPopupWindow.setOutsideTouchable(outsideTouchable);
        return (T) this;
    }

    public PopupWindowUtils setFocusableInTouchMode(boolean focusableInTouchMode) {
        mPopupWindowView.setFocusableInTouchMode(focusableInTouchMode);
        return this;
    }

    public boolean hidePopupWindow(boolean ifUserCancel) {
        if (null != mPopupWindow && mPopupWindow.isShowing()) {
            mPopupWindow.dismiss();
            mContext = null;//防止引用activity导致内存泄漏
            if (null != mCallBack) {
                mCallBack.onPopupWindowHided(ifUserCancel);
            }
            return true;
        }
        return false;
    }

    public interface PopupWindowUtilsCallBack {
        void onPopupWindowHided(boolean ifUserCancel);
        void onPopupWindowClickPositiveBtn();
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == mMainId) {
        } else if (v.getId() == mRootId) {
            hidePopupWindow(true);
        } else if (v.getId() == R.id.popup_positive_btn) {
            hidePopupWindow(false);
            if (null != mCallBack) {
                mCallBack.onPopupWindowClickPositiveBtn();
            }
        }
    }

}
